package com.us.usws.site.ws.entity.channel;

import java.util.ArrayList;
import java.util.List;

import com.us.usws.biz.VideoRateStartWorker;
import com.us.usws.biz.WSResourceURIReplacer;
import com.us.usws.model.domain.base.BaseVideoCat;
import com.us.usws.model.domain.system.User;
import com.us.usws.model.domain.video.Video;
import com.us.usws.model.domain.video.VideoExtattr;
import com.us.usws.model.flat.base.DOBaseVideoCat;
import com.us.usws.service.ro.UserROService;
import com.us.usws.site.ws.entity.channel.video.WSChannelVideoAttr;
import com.us.usws.site.ws.entity.channel.video.WSChannelVideoCat;
import com.us.usws.site.ws.entity.channel.video.WSChannelVideoDetail;
import com.us.usws.site.ws.entity.channel.video.WSChannelVideoUser;
import com.us.usws.support.ext.CacheManager;
import com.us.usws.support.uitls.ServiceFactory;

public class PackEntityConverter {

	/**
	 * get channel video detail
	 * @param v
	 * @return
	 */
	public static WSChannelVideoDetail genWSChannelVideoDetail(Video v) {
		WSChannelVideoDetail ret = new WSChannelVideoDetail();
		ret.setUid(v.getUid());
		ret.setName(v.getName());
		ret.setDescription(v.getDescription());
		ret.setPlayLen(v.getVideoFile().getPlayLen());
		ret.setPlayCnt(v.getPlayCnt());
		ret.setAttendeeCnt(v.getAttendees() == null ? 0 : v.getAttendees().size());
		ret.setRateStarNum(VideoRateStartWorker.getRateStar(v.getPlayCnt()));
		ret.setVideoInfoUrl(WSResourceURIReplacer.getVideoInfoURL(v.getUid()));
		ret.setSnapshot(WSResourceURIReplacer.getVideoSnapshotURL(v.getVideoFile().getUid(), v.getVideoFile().getSnapshot()));

		//categories
		if (v.getCategories() != null) {
			List<WSChannelVideoCat> categories = new ArrayList<WSChannelVideoCat>();
			for (BaseVideoCat c : v.getCategories()) {
				categories.add(genOneChannelVideoCat(c));
			}
			ret.setCategories(categories);
		}

		//attributes
		if (v.getExtraAttributes() != null) {
			List<WSChannelVideoAttr> attributes = new ArrayList<WSChannelVideoAttr>();
			for (VideoExtattr e : v.getExtraAttributes()) {
				String euid = e.getExtattrUid();
				String elabel = CacheManager.getVideoTypeExtattrCache().getLabel(euid);
				attributes.add(new WSChannelVideoAttr(elabel, e.getValue()));
			}
			ret.setAttributes(attributes);
		}

		//user
		UserROService service = ServiceFactory.getInstance().getService(UserROService.class);
		WSChannelVideoUser user = new WSChannelVideoUser();

		String createByUid = v.getCreateByUid();
		User creator = service.getUser(createByUid);
		user.setName(creator.getFullName());
		user.setAvatar(WSResourceURIReplacer.getUserAvatar(creator.getUid(), creator.getAvatar()));
		user.setUrl(WSResourceURIReplacer.getUserUrl(creator.getUid()));
		ret.setUser(user);

		//attendee
		ret.setAttendeeCnt(v.getAttendees() == null ? 0 : v.getAttendees().size());
		ret.setApplyUrl(WSResourceURIReplacer.getVideoAttendeeUrl(v.getUid()));

		//return
		return ret;
	}

	/**
	 * private get video cat
	 * @param cat
	 * @return
	 */
	private static WSChannelVideoCat genOneChannelVideoCat(DOBaseVideoCat cat) {
		WSChannelVideoCat ret = new WSChannelVideoCat(cat);
		ret.setUrl(WSResourceURIReplacer.getCategoryVideoListURL(cat.getUid()));
		return ret;
	}

}
